<?php

require($_SERVER['DOCUMENT_ROOT'] . "/bootstrap.php");

if (isset($_GET['idproduct']))
	$idproduct = $_GET['idproduct'];
else
	$idproduct = 0;

$productdetail1 = mysql_query(@"
        SELECT *
        FROM product p
        INNER JOIN category c
            ON p.`idcategory` = c.`idcategory`
        WHERE idproduct = $idproduct
    ");
$found = mysql_num_rows($productdetail1);
$smarty->assign("found", $found);
if ($found == 0) {
	$smarty->display('../templates/productdetail/index.tpl');
	die();
}
while ($row = mysql_fetch_array($productdetail1)) {
	$productdetail[] = $row;
}
$smarty->assign("productdetail", $productdetail);

// load category
$sqlcategory = mysql_query("select * from category where parent!=0");
while ($row = mysql_fetch_array($sqlcategory)) {
	$resultcategory[] = $row;
}

$smarty->assign("resultcategory", $resultcategory);

//load xong catrgory
$productdetailcolor1 = mysql_query(@"
        SELECT *
        FROM product_color pc
        INNER JOIN color c
            ON pc.`idcolor` = c.`idcolor`
        WHERE idproduct = $idproduct
    ");
while ($row = mysql_fetch_array($productdetailcolor1)) {
	$productdetailcolor[] = $row['idcolor'];
}

if (mysql_num_rows($productdetailcolor1) != 0) {
	$smarty->assign("productdetailcolor", $productdetailcolor);
}


$idcategory = $productdetail[0]['idcategory'];
$productcategoryparent1 = mysql_query(@"
        SELECT c1.`categoryname`, c1.`link`, c2.`categoryname` AS parentname, c2.`link` AS parentlink
        FROM category c1, category c2
        WHERE c1.`parent` = c2.`idcategory` AND c1.`idcategory` = $idcategory
    ");
while ($row = mysql_fetch_array($productcategoryparent1)) {
	$productcategoryparent[] = $row;
}
$sqlcolor = mysql_query("select * from color");
while ($row = mysql_fetch_array($sqlcolor)) {
	$resultcolor[] = $row;
}

$smarty->assign("resultcolor", $resultcolor);
$smarty->assign("productcategoryparent", $productcategoryparent);



if (isset($_POST['editproduct'])) {
	$idcategoryedit = $_POST['idcategory'];
	$prieceedit = $_POST['priece'];
	$prieceedit = str_replace(",", "", $prieceedit);
	$priece_company_edit = $_POST['priece_company'];
	$priece_company_edit = str_replace(",", "", $priece_company_edit);
	$productnamedit = $_POST['productname'];
	$promotionedit = $_POST['promotion'];
	$warrantyedit = $_POST['warranty'];

	$coloredit = $_POST['color'];
	
	$inventoryedit = $_POST['inventory'];
	$productdetailedit = $_POST['inventory'];

	$updateproduct = mysql_query(@"update product set idcategory=$idcategoryedit, price = $prieceedit,price_company=$priece_company_edit,
									productname='$productnamedit',promotion='$promotionedit',warranty=$warrantyedit,inventory=$inventoryedit
									where idproduct=$idproduct");
	mysql_query(@"delete from product_color where idproduct=$idproduct");
	foreach ($coloredit as $key => $value) {
		mysql_query(@"insert into product_color(idproduct,idcolor) value($idproduct,$value)");
		
	}
	echo '<script type="text/javascript">';
	echo 'alert("Lưu thành công!");';
	echo 'setTimeout("top.location.href =' . "'/editproduct/$idproduct'" . '",0);';
	echo "</script>";
	die();
}
$smarty->display('../templates/editproduct/editproduct.tpl');
?>